package com.acme.bankapp.service.bank;

import java.io.File;
import java.io.IOException;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.exceptions.ClientExistsException;
import com.acme.bankapp.exceptions.FeedLoaderException;
import com.acme.bankapp.exceptions.IllegalAmountArgumentException;
import com.acme.mock.BankServer;


public class BankAplication {
	
	public static File dataBaseFile = new File ("bankSerial.dbs");
	
	public static void main(String[] args) throws IllegalAmountArgumentException, ClientExistsException, FeedLoaderException, IOException, ClassNotFoundException {
		
		try {	
			Bank deutscheBank = new Bank("Deutsche Bank");
			
			for(int i=0; i<args.length; i++){
				if (args[i].equals("-loadfeed") && i+1 < args.length ){
					File feedFile = new File( args[i+1] );
					if (feedFile.isFile()){
						BankDataLoaderService.getClientsFromFeed(deutscheBank, feedFile);
					}else{
						System.out.println( feedFile.getAbsolutePath() + " is missing");
					}
					System.out.println("\nfeedFile loaded");
					BankService.showBank(deutscheBank);
				}
				if (args[i].equals("-loadbank") && i+1 < args.length ){
					if (new File( args[i+1] ).isFile()){
						dataBaseFile = new File( args[i+1] );
						deutscheBank = BankService.readBank(dataBaseFile);
					}else{
						System.out.println( dataBaseFile.getAbsolutePath() + " is missing");
					}
					System.out.println("\ndataBase loaded");
					BankService.showBank(deutscheBank);
				}
				if (args[i].equals("-server") ){
					BankServer server = new BankServer();
					while (true) {
						server.run(deutscheBank);
					}
				}
			}
	
		} catch (NullPointerException nullExc) {
			System.out.println(nullExc.getMessage());	
		}
	}
}